package com.seeyon.apps.bense.bank.sync;

import cn.hutool.core.lang.Assert;
import com.seeyon.apps.bense.bank.core.Constants;
import com.seeyon.apps.bense.bank.manager.ZongZhangPayManagerImpl;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.quartz.QuartzHolder;
import com.seeyon.ctp.common.quartz.QuartzJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务-差旅费报销
 */
public class ZongZhangPayTask implements QuartzJob {

    private final static Log logger = LogFactory.getLog(ZongZhangPayTask.class);

    /**
     * 间隔同步的小时和分钟数（默认为:10分钟）
     */
    private static int intervalHour = 10;

    private static int intervalMin = 10;

    private static int triggerHour = 23;

    private static int triggerMinute = 10;

    /**
     * 定义的任务插件ID
     */
    private static String jobBeanId = "zongZhangPayTask";

    /**
     * 定义的任务名称
     */
    private static String jobName = "QuartzSyncZongZhangPayBack";

    private ZongZhangPayManagerImpl impl = (ZongZhangPayManagerImpl) AppContext.getBean("zongZhangPayManagerImpl");

    @Override
    public void execute(Map<String, String> map) {
        logger.info("***拉取宁波银行总账手续费同步事件开始**");
        try {
            impl.doSync();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("银行总账手续费同步任务发生异常", e);
            e.printStackTrace();
        }

        logger.info("***银行总账手续费同步事件结束**");
    }

    /**
     * 注册同步任务（V8.0）
     */
    public static void registerTask(boolean canExcute) {
        logger.info("银行总账手续费同步调度任务" + ZongZhangPayTask.class.getSimpleName() + "...");
        try {
            if (!canExcute) {
                QuartzHolder.deleteQuartzJob(jobName);
                return;
            }
            // 先删除任务
            QuartzHolder.deleteQuartzJob(jobName);
//            // 设定时间
//            Map<String, String> parameters = new HashMap<String, String>();
//            String cron = Constants.travelPaymentCron;
//            Assert.notBlank(cron, "银行总账手续费定时cron表达式不能为空");
//            QuartzHolder.newCronQuartzJob(null, jobName, cron, new Date(), null, jobBeanId, parameters);
            logger.info("银行总账手续费同步任务" + ZongZhangPayTask.class.getSimpleName() + ",成功");
			// 设定时间
			Map<String, String> parameters = new HashMap<String, String>();
			// 设置时间
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date()); // 年月日 也可以具体到时分秒如calendar.set(2015, 10, 12,11,32,52);
											// 每日运行，只要改动 时间即可
//			calendar.set(Calendar.MONTH, 5);//月份
//			calendar.set(Calendar.DATE, 12);//日期
			calendar.set(Calendar.HOUR_OF_DAY, triggerHour);//小时
			calendar.set(Calendar.MINUTE, triggerMinute);//分钟
			calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 5);
			Date date = calendar.getTime();// date就是你需要的时间
			// 间隔时间
			int delta = (intervalHour * 60 + intervalMin) * 60 * 1000;
            QuartzHolder.newQuartzJob(jobName, new Date(),delta,jobBeanId, parameters);
            
            //每天定时
//			QuartzHolder.newQuartzJobPerDay(null, jobName, date, jobBeanId, parameters);
        
        
        } catch (Exception e) {
            logger.error("银行总账手续费同步调度任务发生异常", e);
        }
    }
}
